﻿using System;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Text;
using SpiderRelation0720.Help;
using SpiderRelation0720.DbUpdate;
using System.Collections.Generic;
using SpiderRelation0720.WebSearch;
public class Test
{
    public static void Main()
    {
        #region 测试
        //基本测试结果通过
        /* string building = "1-5";
         int i=BuildingDictionary.GetTeacherBuildingId(building);
         Console.WriteLine("{0}:对应的编号为：{1}",building,i);
         List<RCourseRecord> record = new List<RCourseRecord>();
         RCourseRecord first = new RCourseRecord();
         first.area = 1;
         first.buildingMark = "5";
         first.classRoom = 302;
         first.dayOfTheWeek = 3;
         first.startOfWeek = 1;
         first.endOfWeek = 18;
         first.startNumberOfClass = 3;
         first.endNumberOfClass = 4;
         first.floorNumber = 2;
         record.Add(first);
         DbUpdateControl control = new DbUpdateControl();
         bool b=control.AddCourseRecord(record);
         Console.WriteLine("更新的结果为：{0}", b);*/
        /*  string result = GetPageData("http://202.114.74.198/stu/choose_pubLesson_list_beta.jsp?actionType=choose", "");
          Console.WriteLine(result);
          string path = @"C:\Users\CLN_2012\Desktop\教务部网站.txt";
        //创建并写入(将覆盖已有文件)
      // if (!File.Exists(path))
       //{
           using (StreamWriter sw = File.CreateText(path))
           {
               sw.WriteLine(result);
           }
       //}*/
        #endregion
        WebSearch search = new WebSearch();
        DbUpdateControl db = new DbUpdateControl();
        for (int i = 1; i < 9; i++)
        {
            string url = "http://202.114.74.198/stu/choose_pubLesson_list_beta.jsp?actionType=choose&pageNum="+i.ToString();
            string cookie = "	JSESSIONID=0000iCPmgnATybXaCdeg6ZIAO9E:-1";
            string text = search.GetPageData(url, "", cookie);
            List<RCourseRecord> result = search.Anaylze(text);
            Console.Write(text);
            Console.Write("获取的记录是：{0}", result.Count);
            int b = db.AddCourseRecord(result);
            Console.WriteLine("第{0}次正确插入的课程记录条数：" + b.ToString(),i);
        }

    }
    /*  public  static  string GetPageData(string url, string charSet)
      {
          string strWebData = string.Empty;
          if (url != null || url.Trim() != "")
          {
              WebClient myWebClient = new WebClient();
              //创建WebClient实例myWebClient
              // 需要注意的：
              //有的网页可能下不下来，有种种原因比如需要cookie,编码问题等等
              //这是就要具体问题具体分析比如在头部加入cookie
              myWebClient.Headers.Add("Cookie", "JSESSIONID=0000b09vk-QTDbPJUkJ2qgkZjy3:-1");
             // myWebClient.Headers.Add("请求", "GET /stu/newstu_index.jsp HTTP/1.1");
              myWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU");
              //这样可能需要一些重载方法。根据需要写就可以了
              //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
              myWebClient.Credentials = CredentialCache.DefaultCredentials;
              //如果服务器要验证用户名,密码
              //NetworkCredential mycred = new NetworkCredential("2010302580222", "19920501");
              //myWebClient.Credentials = mycred;
              //从资源下载数据并返回字节数组。（加@是因为网址中间有"/"符号）
              byte[] myDataBuffer = myWebClient.DownloadData(url);
              strWebData = Encoding.Default.GetString(myDataBuffer);
              //获取网页字符编码描述信息
              Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
              string webCharSet = charSetMatch.Groups[2].Value;
              if (charSet == null || charSet == "")
              {
                  //如果未获取到编码，则设置默认编码
                  if (webCharSet == null || webCharSet == "")
                  {
                      charSet = "UTF-8";
                  }
                  else
                  {
                      charSet = webCharSet;
                  }
              }
              if (charSet != null && charSet != "" && Encoding.GetEncoding(charSet) != Encoding.Default)
              {
                  strWebData = Encoding.GetEncoding(charSet).GetString(myDataBuffer);
              }
          }
          return strWebData;
      }
  }
  */
}